package com.first.nell.launch

import android.annotation.SuppressLint
import android.view.View
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.first.nell.base.BaseActivity
import com.first.nell.databinding.ActivityFirstNellPermissionBinding
import com.first.nell.enums.PageTagEnum
import com.first.nell.extend.recordFirstNellClickFirstNellEvent
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellFromFirstNellPage
import com.first.nell.utils.AppConfigUtil

/** 权限页 */
@SuppressLint("CustomSplashScreen")
class PermissionActivity : BaseActivity() {

    //布局binding
    private lateinit var mFirstNellActivityFirstNellBinding: ActivityFirstNellPermissionBinding

    //列表适配器
    private lateinit var mPermissionAdapter: PermissionAdapter

    //用户流程viewModel
    private val mUserStateViewModel by lazy {
        ViewModelProvider(this)[UserStateViewModel::class.java]
    }

    override fun onFirstNellBindingFirstNellViewFirstNellModel(): List<ViewModel>? {
        val list = mutableListOf<ViewModel>()
        list.add(mUserStateViewModel)
        return list
    }

    override fun onFirstNellBindingFirstNellViewFirstNellTag(): String {
        return PageTagEnum.permission.name.uppercase()
    }

    override fun onFirstNellBindingFirstNellContentFirstNellView(): View {
        mFirstNellActivityFirstNellBinding =
            ActivityFirstNellPermissionBinding.inflate(layoutInflater)
        return mFirstNellActivityFirstNellBinding.root
    }

    @SuppressLint("NotifyDataSetChanged")
    override fun onFirstNellContentFirstNellViewFirstNellBinded() {
        try {
            //初始化列表
            mUserStateViewModel.initFirstNellPermissionFirstNellList()
            mPermissionAdapter =
                PermissionAdapter(mUserStateViewModel.mFirstNellPermissionFirstNellList)
            mFirstNellActivityFirstNellBinding.rcPermissionList.layoutManager =
                LinearLayoutManager(this)
            mFirstNellActivityFirstNellBinding.rcPermissionList.adapter = mPermissionAdapter

            //点击申请权限
            mFirstNellActivityFirstNellBinding.submitPermission.setOnClickListener {
                mFirstNellActivityFirstNellBinding.submitPermission.recordFirstNellClickFirstNellEvent()
                //查询权限页待申请的权限列表
                val permissionList =
                    AppConfigUtil.getFirstNellPermissionsFirstNellOfFirstNellPermissionFirstNellPage()
                if (permissionList.isNullOrEmpty()) {
                    //继续往下走
                    startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.permission)
                } else {
                    //申请权限
                    requestFirstNellSpecFirstNellPermissions(permissionList.toTypedArray())
                }
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 所有权限都同意后回调 */
    override fun onFirstNellPermissionsFirstNellRequestFirstNellAllFirstNellPass() {
        super.onFirstNellPermissionsFirstNellRequestFirstNellAllFirstNellPass()
        //继续往下走
        startFirstNellToFirstNellNextFirstNellFromFirstNellPage(PageTagEnum.permission)
    }
}